package com.example.feifei.fragment.buy.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.example.feifei.fragment.buy.bean.ShopingBean;
import com.example.feifei.newproject.R;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;


/**
 *  购物车适配器
 * Created by feifei on 2017/12/20.
 */

public class ShopCartAdapter extends RecyclerArrayAdapter<ShopingBean> {

    private OnSelectItemListener selectItemListener;

    public ShopCartAdapter(Context context) {
        super(context);
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolderShop(parent,selectItemListener);
    }

    private class ViewHolderShop extends BaseViewHolder<ShopingBean>{
        CheckBox shop_select;
        TextView shop_name;
        OnSelectItemListener itemListener;
        private boolean flag = false;

        public ViewHolderShop(ViewGroup parent,OnSelectItemListener selectItemListener) {
            super(parent, R.layout.item_shoping_list);
            this.itemListener = selectItemListener;
            shop_select =$(R.id.shop_select);
            shop_name = $(R.id.shop_name);
        }

        @Override
        public void setData(final ShopingBean data) {
            super.setData(data);
            shop_select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    data.setSelect(isChecked);
                }
            });
            shop_select.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   for(int i = 0;i<getItemCount();i++){
                       boolean isSelect = getAllData().get(getPosition()).isSelect();
                       itemListener.checkSelectClick(getPosition(),shop_select.isChecked());
                        if(isSelect){
                            itemListener.checkSelectClick(getPosition(),true);
                        }else{
                            itemListener.checkSelectClick(getPosition(),false);
                        }
                   }
                    notifyDataSetChanged();
                }
            });
            shop_select.setChecked(getAllData().get(getPosition()).isSelect());

        }
    }

    public void setOnSelectItemListener(OnSelectItemListener listener){
        this.selectItemListener = listener;
    }

    public interface OnSelectItemListener{
        void checkSelectClick(int position, boolean isSelect);
    }

    public void notifyData(){

    }
}
